package Classic150;

import java.util.ArrayList;
import java.util.List;

public class 电话号码的字母组合 {

    String digits;
    String[] letters = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    StringBuilder sb = new StringBuilder();
    List<String> res = new ArrayList<>();

    /*
        回溯的模板作答
     */
    public List<String> letterCombinations(String digits) {
        if (digits.isEmpty()) return res;
        this.digits = digits;
        backTracking(0);
        return res;
    }

    private void backTracking(int index) {
        if (index == digits.length()) {
            res.add(sb.toString());
            return;
        }
        String letter = letters[digits.charAt(index) - '2'];
        for (int i = 0; i < letter.length(); i++) {
            sb.append(letter.charAt(i));
            backTracking(index + 1);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}
